<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;销售管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;客户管理</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="categoryForm"
            :rules="rules"
            ref="categoryForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="商品代码:" prop="categoryId">
                <el-input v-model="categoryForm.categoryId" placeholder="请输入商品代码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="商品名称:" prop="name">
                <el-input v-model="categoryForm.name" placeholder="输入商品名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider content-position="left">金额计算</el-divider>
          <el-form-item label="上月结转:" prop="lmonth">
            <el-input v-model="categoryForm.lmonth" placeholder="输入上月结转"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="8">
              <el-form-item label="本月入库:" prop="tmonthIn">
                <el-input v-model="categoryForm.tmonthIn" placeholder="输入本月入库"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="本月出库:" prop="tmonthOut">
                <el-input v-model="categoryForm.tmonthOut" placeholder="输入本月出库"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="当前数目:" prop="countNow">
                <el-input v-model="categoryForm.countNow" placeholder="输入当前数目"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="标准库存数:" prop="count">
                <el-input v-model="categoryForm.count" placeholder="输入标准库存数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="溢短:" prop="overflowShort">
                <el-input v-model="categoryForm.overflowShort" placeholder="输入溢短"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider content-position="left">库存金额</el-divider>
          <el-row>
           <el-col :span="12">
             <el-form-item label="单价:" prop="unitPrice">
               <el-input v-model="categoryForm.unitPrice" placeholder="输入单价"></el-input>
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item label="成本:" prop="primeCost">
               <el-input v-model="categoryForm.primeCost" placeholder="输入成本"></el-input>
             </el-form-item>
           </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item label="库存金额:" prop="inventoryAmount">
                <el-input v-model="categoryForm.inventoryAmount" placeholder="输入库存金额"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item style="float: right">
                <el-button type="primary"@click="compute" style="background-color: white;border-color: gray">
                  <i class="el-icon-thumb" style="color:gray;font-size: 100%">&nbsp;&nbsp;计算</i>
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button type="primary" @click="submitForm('categoryForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('categoryForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      categoryForm: {
        id:'',
        categoryId: 123,
        name: "药品",
        lmonth: 1200000,
        tmonthIn: 250,
        tmonthOut: 125,
        countNow: 114,
        count: 0,//标准库存数 = 本月入 - 本月出
        overflowShort: 0,//溢短 = 本月出 - (库存数 - 当前库存数)
        unitPrice: 50,
        primeCost: 25,
        inventoryAmount: 0//库存金额 = 当前库存数 * 成本
      },
      rules: {
        categoryId: [
          {required: true, message: "请输入商品代码", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        name: [
          {required: true, message: "请输入商品名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        lmonth: [
          {required: true, message: "请输入上月结转", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        tmonthIn: [
          {required: true, message: "请输入本月入库", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        tmonthOut: [
          {required: true, message: "请输入本月出库", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        countNow: [
          {required: true, message: "请输入当前数目", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        count: [
          {required: true, message: "请输入标准库存数", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        overflowShort: [
          {required: true, message: "请输入溢短", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        unitPrice: [
          {required: true, message: "请输入单价", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        primeCost: [
          {required: true, message: "请输入成本", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        inventoryAmount: [
          {required: true, message: "请输入库存金额", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    compute() {
      let count = this.categoryForm.tmonthIn - this.categoryForm.tmonthOut;
      let overflowShort = this.categoryForm.tmonthOut - (this.categoryForm.tmonthIn - this.categoryForm.countNow);
      let inventory = this.categoryForm.countNow * this.categoryForm.primeCost;
      if(overflowShort<0){
        overflowShort = -overflowShort;
      }
      this.$set(this.categoryForm,'count',count);
      this.$set(this.categoryForm,'overflowShort',overflowShort);
      this.$set(this.categoryForm,'inventoryAmount',inventory);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/wareHouseCategory/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.categoryForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
}

</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}
</style>